
package com.wzt.lianfirecontrol.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.bean.recode.home.ChartsModel;
import com.wzt.lianfirecontrol.bean.recode.home.ChartsModel2;
import com.wzt.lianfirecontrol.utils.StringUtils;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
@SuppressLint("ViewConstructor")
public class MyMarkerView extends MarkerView {
    private Context context;
    private final TextView tvTitle;
    private final TextView tvContent;
    private final ChartsModel2 chartsModel;
    private final boolean isDetail;

    public MyMarkerView(Context context, int layoutResource, ChartsModel2 chartsModel2, boolean isDetail) {
        super(context, layoutResource);
        this.context = context;
        this.chartsModel = chartsModel2;
        this.isDetail = isDetail;
        tvTitle = findViewById(R.id.tvTitle);
        tvContent = findViewById(R.id.tvContent);
    }

    // runs every time the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;
            if (isDetail){
                try {
                    tvTitle.setText(chartsModel.getxAxis().get((int)e.getX()).substring(11, 16));
                    tvTitle.setVisibility(VISIBLE);
                } catch (Exception e1) {
                    tvTitle.setVisibility(GONE);
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(chartsModel.getyAxis().get((int) e.getX()));
                if (!"0".equals(chartsModel.getIsPercentage())){
                    stringBuilder.append("%");
                }
                if (!StringUtils.isEmpty(chartsModel.getUnit())){
                    stringBuilder.append(chartsModel.getUnit());
                }
                tvContent.setText(stringBuilder.toString());
                tvContent.setTextSize(14);
                tvContent.setPadding(0, 0, 0, com.wzt.lianfirecontrol.utils.Utils.dip2px(context, 5));
            }else {
                try {
                    tvTitle.setText(chartsModel.getHomeTendModels().get(Integer.parseInt(e.getX() + "")).getWarnTime());
                    tvTitle.setVisibility(VISIBLE);
                } catch (Exception e1) {
                    tvTitle.setVisibility(GONE);
                }
                tvContent.setText(chartsModel.getChartTitle() + ": " + chartsModel.getyAxis().get((int) e.getX()));
            }
        } else {
            if (isDetail){
                try {
                    tvTitle.setText(chartsModel.getxAxis().get((int)e.getX()).substring(11, 16));
                    tvTitle.setVisibility(VISIBLE);
                } catch (Exception e1) {
                    tvTitle.setVisibility(GONE);
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(chartsModel.getyAxis().get((int) e.getX()));
                if (!"0".equals(chartsModel.getIsPercentage())){
                    stringBuilder.append("%");
                }
                if (!StringUtils.isEmpty(chartsModel.getUnit())){
                    stringBuilder.append(chartsModel.getUnit());
                }
                tvContent.setText(stringBuilder.toString());
                tvContent.setTextSize(14);
                tvContent.setPadding(0, 0, 0, com.wzt.lianfirecontrol.utils.Utils.dip2px(context, 5));
            }else {
                try {
                    tvTitle.setText(chartsModel.getHomeTendModels().get((int) e.getX()).getWarnTime());
                    tvTitle.setVisibility(VISIBLE);
                } catch (Exception e1) {
                    tvTitle.setVisibility(GONE);
                }
                tvContent.setText(chartsModel.getChartTitle() + ": " + chartsModel.getyAxis().get((int) e.getX()));
            }
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
